package com.sunmnet.j2ee.action.powerdb.designer;

import com.sunmnet.j2ee.action.BaseAction;
import com.sunmnet.j2ee.action.BaseNewAction;
import com.sunmnet.j2ee.bean.pub.action.JsonResult;
import com.sunmnet.j2ee.bean.pub.action.PageResult;
import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.annotation.MethodName;
import com.sunmnet.j2ee.entity.powerdb.designer.PdmModule;
import com.sunmnet.j2ee.service.BaseService;
import com.sunmnet.j2ee.service.powerdb.designer.ModuleService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 蔡岳衡 on 2016/8/4.
 */

@Controller("/powerdb/designer/module")
@RequestMapping("/powerdb/designer/module")
public class ModuleAction extends BaseNewAction<PdmModule, Long> {

	/**
	 * log4j2日志 *
	 */
	private static final Logger LOG = LogManager.getLogger(DbConnInfoAction.class.getName());

	@Override
	@Resource(name = "/powerdb/designer/moduleService")
	public void setService(BaseService<PdmModule, Long> baseService) {
		super.service = baseService;
	}

	/***************** restful start *****************************************************************************/

	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */
	@MethodName(name = "批量查询")
	@RequestMapping(value = "/", method = RequestMethod.PATCH)
	@ResponseBody
	public JsonResult find(HttpServletRequest request) {
		QueryFilter filter = new QueryFilter(request);
		return super.find(filter);
	}

	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */
	@MethodName(name = "批量查询")
	@RequestMapping(value = "/pages", method = RequestMethod.PATCH)
	@ResponseBody
	public PageResult findPages(HttpServletRequest request) {
		QueryFilter filter = new QueryFilter(request);
		return super.findPages(filter);
	}

	/**
	 * 查询
	 *
	 * @param moduleId
	 * @return JsonResult
	 */
	@MethodName(name = "对象查询")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult get(@PathVariable("id") long moduleId, HttpServletRequest request) {

		JsonResult j = new JsonResult(true);
		try {
			List obj = ((ModuleService) service).getModuleById(moduleId);
			if (obj.size() != 0) {
				j.setObj(obj.get(0));
			} else {
				j.setObj(service.get(moduleId));
			}
			j.setMsg("查找实体成功");
		} catch (Exception e) {
			setErrMsg("查找实体失败", LOG, j, e);
		}
		return j;
	}

	/**
	 * 新增
	 *
	 * @param obj
	 * @return JsonResult
	 */
	@MethodName(name = "新增")
	@RequestMapping(value = "/", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult add(PdmModule obj) {
		if (((ModuleService) service).checkRepeat(obj) == 1) {
			JsonResult jsonResult = new JsonResult(false);
			jsonResult.setObj(obj);
			jsonResult.setMsg("模块【" + obj.getName() + "】已经存在，请重新命名！");
			return jsonResult;
		}
		return super.add(obj);
	}

	/**
	 * 更新
	 *
	 * @param obj
	 * @return JsonResult
	 */
	@MethodName(name = "修改")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	@ResponseBody
	public JsonResult update(PdmModule obj) {
		return super.update(obj);
	}


	/**
	 * 删除
	 *
	 * @param id
	 * @return JsonResult
	 */
	@MethodName(name = "删除")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public JsonResult delete(@PathVariable("id") long id) {
		JsonResult j = new JsonResult();
		try {
			((ModuleService) service).deleteModuleRelations(id);
			j = super.delete(id);
		} catch (Exception e) {
			setErrMsg("删除模块失败", LOG, j, e);
		}
		return j;
	}

	/**
	 * 批量删除
	 *
	 * @param ids 一个或多个id "1,2,3"
	 * @return JsonResult
	 */
	@MethodName(name = "批量删除")
	@RequestMapping(value = "/batch", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult batchDelete(@RequestParam(value = "ids", required = true) String ids) {
		return super.delete(ids);
	}

/***************** restful end *****************************************************************************/

	/**
	 * 根据工程id,从模块表获取到关联的表
	 */
	@MethodName(name = "获取工程关联的表")
	@RequestMapping(value = "/getProjectRelModule/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getProjectRelModule(@PathVariable("id") long projectId) {

		JsonResult j = new JsonResult(true);

		try {
			j.setObj(((ModuleService) service).getProjectRelModule(projectId));
			j.setMsg("查询成功！");
		} catch (Exception e) {
			BaseAction.setErrMsg("查询失败", LOG, j, e);
		}
		return j;
	}

	/**
	 * 根据模块id,从实体表获取到关联的表
	 */
	@MethodName(name = "获取模块关联的表")
	@RequestMapping(value = "/getModuleRelEntity/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getModuleRelEntity(@PathVariable("id") long moduleId) {

		JsonResult j = new JsonResult(true);

		try {
			j.setObj(((ModuleService) service).getModuleRelEntity(moduleId));
			j.setMsg("查询成功！");
		} catch (Exception e) {
			BaseAction.setErrMsg("查询失败", LOG, j, e);
		}
		return j;
	}

}
